package com.zhala.web.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhala.common.annotation.Log;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhala.common.core.domain.CommonResult;
import com.zhala.common.core.domain.PageResult;
import com.zhala.common.enums.BusinessType;
import com.zhala.system.domain.*;
import com.zhala.system.domain.vo.*;
import com.zhala.system.service.*;
import com.zhala.web.pasystem.AlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 大屏相关接口
 *
 * @author SSH
 * @version 1.0
 * @description:
 * @date 2024/11/21 20:06
 */
@Api("数据大屏")
@RestController
@RequestMapping("/admin/screen")
public class BigScreenController {
    @Resource
    private IZlSteelPlateService zlSteelPlateService;
    @Resource
    private IZlSteelPipeService iPipeService;
    @Resource
     private IZlSteelBigPipeService zlSteelBigPipeService;
    @Resource
    private ZlHumanNumberStatisticService zlHumanNumberStatisticService;
    @Resource
    private ZlScadaService zlScadaService;
    @Resource
    private ZlAiboxService zlAiboxService;
    @Resource
    private AlarmService alarmService;

    /**
     * 钢板统计
     */
    @GetMapping("/store/statistics")
    public CommonResult<List<PlateMonthStatisticsVO>> statistics() {
        return zlSteelPlateService.monthStoreStatistics();
    }

    /**
     * 管节统计
     */
    @GetMapping("/pip/statistics")
    public CommonResult<PipeCompleteVO> pipCompleteStatic() {
        return iPipeService.pipCompleteStatic();
    }

    /**
     * 月度生成强度折线图
     */
    @GetMapping("/months-accrue-statistics")
    public CommonResult<List<PipeMonthStatisticsVO>> qdStatistics() {
        return iPipeService.qdStatistics(Boolean.FALSE);
    }

    /**
     * 月累积生产管节数柱状图
     */
    @GetMapping("/months-statistics")
    public CommonResult<List<PipeMonthStatisticsVO>> monthsStatistics() {
        return iPipeService.monthsStatistics(Boolean.FALSE);
    }

    /**
     * 正在生产工序
     */
    @GetMapping("/procedure-statistics")
    public CommonResult<PipeProcedureInfo> procedureInfo() {
        return iPipeService.procedureInfo();
    }

    /**
     * 获取所有大管列表
     */
    @Log(title = "获取大管列表", businessType = BusinessType.OTHER)
    @PostMapping("/page")
    public CommonResult<PageResult<List<ZlSteelBigPipe>>> page(@RequestBody @Valid PipeNewPageReqVO reqVO) {
        PageResult<List<ZlSteelBigPipe>> page = iPipeService.newPage(reqVO);
        return CommonResult.success(page);
    }

    /**
     * 通过大管ID获取组成大管的小管
     */
    @Log(title = "通过大管ID获取组成大管的小管", businessType = BusinessType.OTHER)
    @GetMapping("/small-pip")
    public CommonResult<List<SteelPipeRespVO>> listByPipeId(@RequestParam("bigPipeId") String bigPipeId) {
        List<SteelPipeRespVO> List = iPipeService.listByPipeId(bigPipeId);
        return CommonResult.success(List);
    }

    /**
     * 小管详情
     * type:0:获取基础详情 1:带时间线详情
     */
    @GetMapping("/small-pip-info")
    public CommonResult<SteelPipeRespVO> smallPipInfo(@RequestParam("id") String id) {
        return iPipeService.info(id, 1);
    }

   /**
     * 大管详情
     */
    @GetMapping("/big-pip-info")
    @Parameter(name = "id", description = "大管ID", required = false)
    public CommonResult<SteelBigPipeRespVO> bigPipInfo(@RequestParam("id")String id) {
        return zlSteelBigPipeService.info(id,1);
    }


    /**
     * 门禁统计
     */
    private ZlHumanNumberStatisticService service;
    @ApiOperation("门禁统计")
    @GetMapping("/statistic")
    public CommonResult<List<StatisticVO>> statistic() {
        return CommonResult.success(zlHumanNumberStatisticService.statistic());
    }

    /**
     * 门禁记录
     */
    @ApiOperation("门禁记录")
    @GetMapping("/record")
    public CommonResult<List<StatisticFindVO>> record() {
        return CommonResult.success(zlHumanNumberStatisticService.record());
    }


    @ApiOperation("环境监测数据")
    @GetMapping("/get-scada")
    public CommonResult<ZlScada> getScada()
    {
        return CommonResult.success(zlScadaService.getFind());
    }


    @ApiOperation("AI盒子写入")
    @PostMapping("/setAibox")
    public CommonResult<Integer> setaibox(@RequestParam Map<String, Object> map) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 从map中获取resultMsg字段，它是一个JSON格式的字符串
            String resultMsgJson = (String) map.get("resultMsg");
            // 将JSON字符串反序列化为Map对象
            Map<String, Object> resultMsgMap = objectMapper.readValue(resultMsgJson, Map.class);
            // 获取cameraName的值
            String cameraName = (String) resultMsgMap.get("cameraName");
            // 获取algResult字段，它是一个包含label的数组
            String algResultJson = (String) resultMsgMap.get("algResult");
            // 反序列化algResult为数组
            Object[] algResults = objectMapper.readValue(algResultJson, Object[].class);
            // 假设label在第一个元素中，获取第一个元素的Map
            Map<String, String> firstResult = (Map<String, String>) algResults[0];
            // 获取label的值
            String label = firstResult.get("label");
            // 设置ZlAibox对象的属性
            ZlAibox zlAibox = new ZlAibox();
            zlAibox.setCamera_name(cameraName); // 确保ZlAibox类有这个方法
            zlAibox.setType(label); // 确保ZlAibox类有这个方法
            alarmService.addAlarmTask(label);//写入报警信息
            // 保存zlAibox对象并返回结果
            return CommonResult.success(zlAiboxService.save(zlAibox));
        } catch (Exception e) {
            // 处理异常，例如返回错误信息
            e.printStackTrace();
            return CommonResult.error("Error parsing resultMsg");
        }
    }



    /**
     * 只获取10分钟内报警的信息，10分钟后自动结束
     */
    @ApiOperation("Ai盒子获取")
    @GetMapping("/getAibox")
    public CommonResult<List<ZlAiboxReqVo>> getaibox() {
        return CommonResult.success(zlAiboxService.getList());
    }
}
